const express = require('express')
const app = express()
const mongoose = require('mongoose')
const db = require('./config/keys').mongoURL
const bodyParser = require('body-parser')
const passport = require('passport')

//引入users.js/profiles.js 接口
const users = require('./routers/api/users')
const profiles = require('./routers/api/profiles')

//Connect to Mongodb
mongoose.connect(db).then(() => {
  console.log('MongoDb Connect Success');
}).catch((err) => {
  console.log(err);
})

app.get("/", (req,res) => {
  res.send("Hello World!")
})

//使用body-parser 中间件
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

//passport初始化
app.use(passport.initialize())
require('./config/passport')(passport)

//使用routes
app.use('/api/users',users)
app.use('/api/profiles',profiles)

const port = process.env.PORT || 5000

app.listen(port, () => {
  console.log(`Server run at "http://localhost:${port}"`)
})